package team.freeroom.hotelmanage.controller;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import team.freeroom.hotelmanage.controller.model.Page;
import team.freeroom.hotelmanage.repository.entity.Account;
import team.freeroom.hotelmanage.service.AccountService;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("/account")
@Controller
public class AccountController {

    @Autowired
    private AccountService accountService;


    /**
     * 客户管理列表页面
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView list(ModelAndView model) {
        model.setViewName("resident/account");
        return model;
    }

    /**
     * 客户信息添加操作
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> add(Account account) {
        Map<String, String> ret = new HashMap<String, String>();
        if (account == null) {
            ret.put("type", "error");
            ret.put("msg", "请填写正确的客户信息!");
            return ret;
        }
        if (StringUtils.isEmpty(account.getName())) {
            ret.put("type", "error");
            ret.put("msg", "客户名称不能为空!");
            return ret;
        }
        if (isExist(account.getName(), 0l)) {
            ret.put("type", "error");
            ret.put("msg", "该用户名已经存在!");
            return ret;
        }
        if (accountService.add(account) <= 0) {
            ret.put("type", "error");
            ret.put("msg", "添加失败，请联系管理员!");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "添加成功!");
        return ret;
    }

    /**
     * 客户信息编辑操作
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> edit(Account account) {
        Map<String, String> ret = new HashMap<String, String>();
        if (account == null) {
            ret.put("type", "error");
            ret.put("msg", "请填写正确的客户信息!");
            return ret;
        }
        if (StringUtils.isEmpty(account.getName())) {
            ret.put("type", "error");
            ret.put("msg", "客户名称不能为空!");
            return ret;
        }
        if (isExist(account.getName(), account.getId())) {
            ret.put("type", "error");
            ret.put("msg", "该用户名已经存在!");
            return ret;
        }
        if (accountService.edit(account) <= 0) {
            ret.put("type", "error");
            ret.put("msg", "添加失败，请联系管理员!");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "修改成功!");
        return ret;
    }

    /**
     * 分页查询客户信息
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> list(
            @RequestParam(name = "name", defaultValue = "") String name,
            @RequestParam(name = "realName", defaultValue = "") String realName,
            @RequestParam(name = "idCard", defaultValue = "") String idCard,
            @RequestParam(name = "mobile", defaultValue = "") String mobile,
            @RequestParam(name = "status", required = false) Integer status,
            Page page
    ) {
        Map<String, Object> ret = new HashMap<String, Object>();
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("name", name);
        queryMap.put("status", status);
        queryMap.put("realName", realName);
        queryMap.put("idCard", idCard);
        queryMap.put("mobile", mobile);
        queryMap.put("offset", page.getOffset());
        queryMap.put("pageSize", page.getRows());
        ret.put("rows", accountService.findList(queryMap));
        ret.put("total", accountService.getTotal(queryMap));
        return ret;
    }

    /**
     * 客户信息删除操作
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> delete(Long id) {
        Map<String, String> ret = new HashMap<String, String>();
        if (id == null) {
            ret.put("type", "error");
            ret.put("msg", "请选择要删除的信息!");
            return ret;
        }
        try {
            if (accountService.delete(id) <= 0) {
                ret.put("type", "error");
                ret.put("msg", "删除失败，请联系管理员!");
                return ret;
            }
        } catch (Exception e) {
            ret.put("type", "error");
            ret.put("msg", "该客户下存在订单信息，请先删除该客户下的所有订单信息!");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "删除成功!");
        return ret;
    }

    /**
     * 判断用户名是否存在
     */
    private boolean isExist(String name, Long id) {
        Account findByName = accountService.findByName(name);
        if (findByName == null) return false;
        if (findByName.getId().longValue() == id.longValue()) return false;
        return true;
    }
}
